home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libgutil / bufpool.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  142 lines

  1. /*
  2.  * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /*
  19.  *  bufpool.c 
  20.  *
  21.  *  $Revision: 1.2 $
  22.  */
  23. #include "stdio.h"
  24. #include "assert.h"
  25. #include "bufpool.h"
  26.  
  27. /* local functions */
  28. static void    grow_pool( register Pool * );
  29.  
  30. /*-----------------------------------------------------------------------------
  31.  * new_pool - allocate a new pool of buffers
  32.  *-----------------------------------------------------------------------------
  33.  */
  34. Pool *
  35. new_pool( int buffersize, int initpoolsize, char *name )
  36. {
  37.     register Pool *p;
  38.  
  39.     p        = (Pool *) mymalloc( sizeof (Pool) );
  40.     p->buffersize= (buffersize < sizeof(Buffer)) ? sizeof(Buffer)    
  41.                              : buffersize;
  42.     p->nextsize    = initpoolsize * p->buffersize;
  43. #ifndef NDEBUG
  44.     p->name    = name;
  45.     p->magic    = is_allocated;
  46. #endif
  47.     p->nextblock= 0;
  48.     p->curblock    = 0;
  49.     p->freelist    = 0;
  50.     p->nextfree    = 0;
  51.     return p;
  52. }
  53.  
  54. /*-----------------------------------------------------------------------------
  55.  * new_buffer - allocate a buffer from a pool
  56.  *-----------------------------------------------------------------------------
  57.  */
  58.  
  59. char *
  60. new_buffer( register Pool *p )
  61. {
  62.     char *buffer;
  63.  
  64.     assert( p && (p->magic == is_allocated) );
  65.  
  66.     /* find free buffer */
  67.  
  68.     if( p->freelist ) {
  69.         buffer = (char *) p->freelist; 
  70.         p->freelist = p->freelist->next;
  71.     } else {
  72.         if( ! p->nextfree )
  73.             grow_pool( p );
  74.         p->nextfree -= p->buffersize;;
  75.         buffer = p->curblock + p->nextfree;
  76.     }
  77.     return buffer;
  78. }
  79.  
  80. static void
  81. grow_pool( register Pool *p )
  82. {
  83.     assert( p && (p->magic == is_allocated) );
  84.     p->curblock = (char *) mymalloc( p->nextsize );
  85.     p->blocklist[p->nextblock++] = p->curblock;
  86.     p->nextfree = p->nextsize;
  87.     p->nextsize *= 2;
  88. }
  89.  
  90. /*-----------------------------------------------------------------------------
  91.  * free_buffer - return a buffer to a pool
  92.  *-----------------------------------------------------------------------------
  93.  */
  94.  
  95. void
  96. free_buffer( Pool *p, void *b )
  97. {
  98.     assert( p && (p->magic == is_allocated) );
  99.  
  100.     /* add buffer to singly connected free list */
  101.  
  102.     ((Buffer *) b)->next = p->freelist;
  103.     p->freelist = (Buffer *) b;
  104. }
  105.  
  106. /*-----------------------------------------------------------------------------
  107.  * free_pool - free a pool of buffers and the pool itself
  108.  *-----------------------------------------------------------------------------
  109.  */
  110.  
  111. void 
  112. free_pool( Pool *p )
  113. {
  114.     assert( p && (p->magic == is_allocated) );
  115.  
  116.     while( p->nextblock )
  117.         free( p->blocklist[--(p->nextblock)] );
  118. #ifndef NDEBUG
  119.     p->magic = is_free;
  120. #endif
  121.     free( p );
  122. }
  123.  
  124. /*-----------------------------------------------------------------------------
  125.  * clear_pool - free buffers associated with pool but keep pool 
  126.  *-----------------------------------------------------------------------------
  127.  */
  128.  
  129. void 
  130. clear_pool( Pool *p )
  131. {
  132.     assert( p && (p->magic == is_allocated) );
  133.  
  134.     while( p->nextblock )
  135.         free( p->blocklist[--(p->nextblock)] );
  136.     p->curblock    = 0;
  137.     p->freelist    = 0;
  138.     p->nextfree    = 0;
  139.     if( p->nextsize >= 2 * p->buffersize )
  140.         p->nextsize /= 2;
  141. }
  142.